﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.Other;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{

    /// <summary>
    /// 仓库逻辑层
    /// </summary>
    public class LKWareHouseBll
    {

        protected LKWareHouseDal dal = new LKWareHouseDal();
        /// <summary>
        /// 获取顶级仓库列表
        /// </summary>
        /// <returns></returns>
        public List<LKWareHouse> GetLKWareHousesTopList()
        {
            //BaseDal<LKWareHouse> dal = new BaseDal<LKWareHouse>();
            var list = dal.GetAll().ToList();
            var parents = list.Where(x => x.ParentId == 0).ToList();

            return parents;
        }


        public List<LKWareHouse> GetLKWareHouses(int? id)
        {
            var list = dal.GetLKWareHouses().ToList();
            int Id = id ?? 0;
            if (Id == 0)
            {
                List<LKWareHouse> parents = list.Where(x => x.ParentId == 0).ToList();
                foreach (var item in parents)
                {
                    if (!list.Exists(x => x.ParentId == item.Id))
                    {
                        continue;
                    }
                    item.state = "closed";
                }
                return parents;
            }
            else
            {
                var childList = list.Where(x => x.ParentId == Id).ToList();
                return childList;
            }
        }

        public List<TreeView> GetHouseComboxTree()
        {
            List<TreeView> tvList = new List<TreeView>();
            //BaseDal<LKWareHouse> dal = new BaseDal<LKWareHouse>();
            var list = dal.GetAll().ToList();
            var parents = list.Where(x => x.ParentId == 0).ToList();
            tvList.Add(new TreeView() { id = 0, text = "顶级", children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in parents)
            {
                tv = new TreeView()
                {
                    id = item.Id,
                    text = item.WareHouseName,
                    children = new List<TreeView>()
                };
                GetTreeChilds(list, item, tv);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }

        public List<TreeView> GetHouseComboxTreeByCompany()
        {
            List<TreeView> tvList = new List<TreeView>();
            if (LoginHelper.GetRoleId() == (int)Roles.管理员) return GetHouseComboxTree();
            var list = dal.GetListByWhere("CompanyId=@companyId", new { companyId = UserContext.CompanyId }).ToList();//GetAll().ToList();
            var parents = list.Where(x => x.ParentId == 0).ToList();
            if (!parents.Any()) 
            {
                parents = list;
            }
            tvList.Add(new TreeView() { id = 0, text = UserContext.CompanyName, children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in parents)
            {
                tv = new TreeView()
                {
                    id = item.Id,
                    text = item.WareHouseName,
                    children = new List<TreeView>()
                };
                GetTreeChilds(list, item, tv);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }

        private void GetTreeChilds(List<LKWareHouse> list, LKWareHouse parent, TreeView tv)
        {
            List<LKWareHouse> newList = list.Where(x => x.ParentId == parent.Id).ToList();
            foreach (var item in newList)
            {
                TreeView newTv = new TreeView()
                {
                    id = item.Id,
                    text = item.WareHouseName,
                    children = new List<TreeView>(),
                };
                GetTreeChilds(list, item, newTv);
                tv.children.Add(newTv);
            }
        }

        private List<LKWareHouse> GetChilds(List<LKWareHouse> list, LKWareHouse parent)
        {
            List<LKWareHouse> childs = list.Where(x => x.ParentId == parent.Id).ToList();
            foreach (var item in childs)
            {
                item.children = new List<LKWareHouse>();
                item.children.AddRange(GetChilds(list, item));
            }
            return childs;
        }

        /// <summary>
        /// 新增仓库
        /// </summary>
        /// <param name="lKWareHouse"></param>
        /// <returns></returns>
        public ReturnModel AddLKWareHouse(LKWareHouse lKWareHouse)
        {
            //LKWareHouseDal dal = new LKWareHouseDal();

            var any = dal.AnyName(lKWareHouse.WareHouseName);
            if (any)
            {
                return new ReturnModel { Success = false, Msg = "仓库名称已存在" };
            }


            lKWareHouse.CreateTime = DateTime.Now;
            lKWareHouse.CreateUserId = LoginHelper.GetUserId();
            lKWareHouse.CreateUser = LoginHelper.UserName();
            var result = dal.Insert(lKWareHouse);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑仓库
        /// </summary>
        /// <param name="lKWareHouse"></param>
        /// <returns></returns>
        public bool EditLKWareHouse(LKWareHouse lKWareHouse)
        {
            //LKWareHouseDal dal = new LKWareHouseDal();
            var cus = dal.Get(lKWareHouse.Id);
            if (cus == null)
            {
                return false;
            }
            lKWareHouse.CreateTime = cus.CreateTime;
            lKWareHouse.CreateUserId = cus.CreateUserId;
            lKWareHouse.CreateUser = cus.CreateUser;
            //lKWareHouse.ModifyUserId = LoginHelper.GetUserId();
            //lKWareHouse.ModifyUser = LoginHelper.UserName();
            //lKWareHouse.ModifyTime = DateTime.Now;
            return dal.Update(lKWareHouse);
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKWareHouse(int id)
        {
            //LKWareHouseDal dal = new LKWareHouseDal();
            return dal.DeleteById(id);
        }
    }
}

